package com.china08.yxyapi.api.manage.stu.mystudy.homework.workinfo;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskQues;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReport;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskQuesRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportRepository;
import com.china08.yxyapi.service.manage.stu.mystudy.homework.dowork.impl.HomeworkServiceImpl;
import com.china08.yxyapi.util.DateUtils;

/***
 * 获取当前任务下的题的列表信息 2017 年3月21日19:47:44**
 * 
 * @author lys
 *
 */
@CrossOrigin
@RestController
public class HomeWorkQuestionListController extends YxyApiBaseController {
    @Autowired
    private HomeworkTaskQuesRepository htr;
    @Autowired
    private HomeworkTaskReportRepository htrr;
    @Autowired
    private HomeworkServiceImpl homeworkSrevice;

    /**
     * 获取当前任务下的题的列表信息 2017年3月21日19:47:44 输入学生ID 和任务ID
     *
     * @param reqModel
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/stu/mystudy/homework/workinfo/homeworklist", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public YxyApiRespModel process(@RequestBody ReqModel reqModel, HttpServletRequest request,
            HttpServletResponse response) {
        // 判断任务ID是否为空
        Assert.notNull(StringUtils.defaultIfBlank(reqModel.homeworkTaskId, null), "任务ID不能为空");
        // 获得数据
        RespModel respModel = new RespModel();
        // 作业
        List<HomeworkTaskQues> hmoeWork = htr.findByTaskId(reqModel.homeworkTaskId);
        respModel.hT = hmoeWork;
        Assert.isTrue(hmoeWork.size() > 0, "没有查到对应的作业！");
        // 答题截止时间
        respModel.predictDate = DateUtils.formatDate3(hmoeWork.get(0).getPredictDate());
        // 根据学生ID和任务id查询学生的任务状态
        HomeworkTaskReport homeworkTaskReport = htrr.findByStudentIdAndTaskId(reqModel.studentId,
                reqModel.homeworkTaskId);
        Assert.notNull(homeworkTaskReport, "当前学生未查到该作业");
        // 如果学生状态是未做，修改学生状态为答题中并保存。

        if (homeworkTaskReport.getStatusOfFinished() == 0) {
            homeworkSrevice.buildAnswers(reqModel.studentId, reqModel.homeworkTaskId);
        }
        return respModel;
    }

    // 返回的对象类型
    protected class RespModel extends YxyApiRespModel {
        // 答题结束时间
        public String predictDate;
        // 当前的题：
        public List<HomeworkTaskQues> hT;

    }

    // 请求的参数类型
    protected static class ReqModel extends YxyApiReqModel {
        // 获得请求的参数（任务id）
        public String homeworkTaskId;
        // 获得当前的学生ID
        public String studentId;
    }
}
